/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package be.wiim.mqtt;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 *
 * @author wimvandecasteele
 */
public class Subscriber extends Client implements MqttCallback{
    
    MqttClient fMyClient;
    MqttConnectOptions fConnectionOptions;
        
    public Subscriber(String pClientId, String pBrokerIP)
    {
        super(pClientId,pBrokerIP);
        runClient();
    }
    
    public Subscriber(String pClientId, String pBrokerIP, String pTopic)
    {
        super(pClientId,pBrokerIP,pTopic);
        runClient();
    }
    
    
    public void runClient() {
        MqttConnectOptions vConnectionOptions = new MqttConnectOptions();
        vConnectionOptions.setCleanSession(false);
        vConnectionOptions.setKeepAliveInterval(3600*24);
        //fConnectionOptions.setUserName(M2MIO_USERNAME);
        //fConnectionOptions.setPassword(M2MIO_PASSWORD_MD5.toCharArray());

        // Connect to Broker
        try {
                fMyClient = new MqttClient(fBrokerUrl,fClientId);
                fMyClient.setCallback(this);
                fMyClient.connect(vConnectionOptions);
        } catch (MqttException ex) {
                ex.printStackTrace();
                System.exit(-1);
        }
        System.out.println("Connected to " + fBrokerUrl);
    }
    
    public void subscribe(String pTopic, int pQos)
    {
        try {
                int vQoS = pQos;
                if(vQoS < 0 || vQoS > 3 )
                    vQoS = 0;
                //Could be 0, 1 or 2
                fMyClient.subscribe(pTopic, vQoS);
        } catch (MqttException ex) {
                ex.printStackTrace();
        }
    }
    
    public void disconnect()
    {
        try {
               fMyClient.disconnect();
           } 
           catch (Exception ex) 
           {
               ex.printStackTrace();
           }
    }

   @Override
   public void connectionLost(Throwable t) {
           System.out.println("Connection lost!");
   }

    @Override
    public void messageArrived(String pTopic, MqttMessage pMM) throws Exception {
        System.out.println("Message Arrived!" );
        System.out.println("Topic: "+pTopic);
        System.out.println("Message: " + pMM.toString());
        System.out.println("________________");
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken pImdt) {
        try {
            System.out.println("Message delivered: " + pImdt.getMessage());
        } catch (MqttException ex) {
            Logger.getLogger(Publisher.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}